//
//  ACEStudyTable.m
//  ACE
//
//  Created by zhangtao on 2018/8/8.
//  Copyright © 2018年 zt.td. All rights reserved.
//

#import "ACEStudyTable.h"
#import "ACEStudyTableViewCell.h"

@implementation ACEStudyTable

-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    if (self = [super initWithFrame:frame style:style]) {
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self registerNib:[UINib nibWithNibName:@"ACEStudyTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
        self.delegate = self;
        self.dataSource = self;
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    //iOS11版本以上,自定义删除按钮高度方法:
    if (@available(iOS 11.0 ,*)) {
        
        for (UIView *subview in self.subviews)
        {
            if([subview isKindOfClass:NSClassFromString(@"UISwipeActionPullView")])
            {
                UIView *swipeActionPullView = subview;
                //1.0修改背景颜色
                swipeActionPullView.backgroundColor =  [UIColor whiteColor];
                //1.1修改背景圆角
                swipeActionPullView.layer.cornerRadius = 5.f;
                swipeActionPullView.layer.masksToBounds = YES;
                
                //2.0修改按钮-颜色
                UIButton *swipeActionStandardBtn = subview.subviews[0];
                if ([swipeActionStandardBtn isKindOfClass:NSClassFromString(@"UISwipeActionStandardButton")]) {
                   
                    CGFloat swipeActionStandardBtnOX = swipeActionStandardBtn.frame.origin.x;
                    CGFloat swipeActionStandardBtnW  = swipeActionStandardBtn.frame.size.width;
                    swipeActionStandardBtn.frame = CGRectMake(swipeActionStandardBtnOX, 10, /*swipeActionStandardBtnW*/100, /*self.cellHeight - 10*/100);
                    //2.1修改按钮背景色
                    swipeActionStandardBtn.backgroundColor =  _actionBgColor;
                    //2.2修改按钮背景圆角
                    swipeActionStandardBtn.layer.cornerRadius = 5.f;
                    swipeActionStandardBtn.layer.masksToBounds = YES;
                    swipeActionStandardBtn.titleLabel.font = [UIFont systemFontOfSize:17];
                    NSString *action = [_actionName stringByReplacingOccurrencesOfString:@" " withString:@""];
                    swipeActionStandardBtn.titleLabel.text = action;
                    //                    swipeActionStandardBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
                }
            }
        }
    }

}





-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 110;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return .1;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [[UIView alloc]init];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ACEStudyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.StudyDelegate && [self.StudyDelegate respondsToSelector:@selector(selectIndex:)]) {
        [self.StudyDelegate selectIndex:indexPath];
    }
}

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

#pragma mark --before ios 11
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return _actionName;
}

-(UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath API_AVAILABLE(ios(11.0)){
    
    UIContextualAction *deleteRowAction =
    [UIContextualAction contextualActionWithStyle:UIContextualActionStyleNormal title:_actionName handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
        
        if(self.StudyDelegate && [self.StudyDelegate respondsToSelector:@selector(actionClickWithIndex:)]){
            [self.StudyDelegate actionClickWithIndex:indexPath];
        }
        completionHandler (YES);
    }];
    
    UISwipeActionsConfiguration *config = [UISwipeActionsConfiguration configurationWithActions:@[deleteRowAction]];
    return config;
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
